package Singleton.demo6;

import java.io.*;

/*
*
* 序列化破坏对象
* */
public class Client {

    public static void main(String[] args) throws Exception {

        writeObjectFile();
        readObjectFromFile();
        readObjectFromFile();

    }
    public static void readObjectFromFile() throws IOException, ClassNotFoundException {
        //1 创建对象输入流对象
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("C:\\Users\\46367\\Desktop\\a.txt"));
        //2 读取对象
        Singleton instance  = (Singleton) inputStream.readObject();

        System.out.println(instance);
        //3 关闭资源
        inputStream.close();

    }

    //向文件中写数据(对象)
    public static void writeObjectFile() throws Exception {
        //1.获取Singleton对象
        Singleton instance = Singleton.getInstance();
        //2.创建对象输出流对象
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\46367\\Desktop\\a.txt"));
        //3.写对象
        outputStream.writeObject(instance);
        //4. 释放资源
        outputStream.close();
    }


}
